package m2_work;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties;

import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.TimeoutException;

public class Producer {
    public static void main(String[] args) throws Exception {
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140");
        f.setPort(5672);
        f.setUsername("admin");
        f.setPassword("admin");
        //f.setVirtualHost("/刘沛霞");
        Channel c = f.newConnection().createChannel();

        //定义队列
        c.queueDeclare(
                "task_queue",
                true,//是否是持久队列
                false,//排他队列,独占队列
                false,//自动删除,没有消费者的时候服务器是不是要删除此队列
                null    //其他属性
        );

        while (true){
            System.out.println("输入消息:");
            String msg = new Scanner(System.in).nextLine();
            c.basicPublish(
                    "",             //m3模式在解释
                    "task_queue",
                    MessageProperties.PERSISTENT_TEXT_PLAIN,           //其他消息属性,可设置消息属性参数
                    msg.getBytes());
        }

    }
}
